﻿var isMonthYearCurrent = currentDate.getFullYear() == year && (currentDate.getMonth() + 1) == month;

function renderChart(tenant, meterTypes, label, containerId) {
    $(label).text("fetching...");

    var startDateTime = new Date(year, month - 1, 1, 0, 0, 0);
    var endDateTime = new Date(new Date(startDateTime).setMonth(startDateTime.getMonth() + 1));

    cloudninja.metering.services.GenericService.getDailyStats(tenant, meterTypes, startDateTime, endDateTime, function (result, data) {
        if (data.length == 0) {
            $(label).text("no data");
            return;
        }

        $(label).text("");

        var series = cloudninja.metering.services.GenericService.prepareDailySeriesData(data, meterTypes);
        cloudninja.metering.charting.ChartRenderer.renderAxes(containerId, series);
    });
}

$(document).ready(function () {
    renderChart(tenantId, ['bwi', 'bwe'], 'chartMesage1', 'chart1');
    renderChart(tenantId, ['dbi', 'dbe'], 'chartMesage2', 'chart2');
    renderChart(tenantId, ['dbs'], 'chartMesage2', 'chart3');
    renderChart(tenantId, ['stg'], 'chartMesage2', 'chart4');

    var maxGaugeValue = 30000;
    var gaugeThreshold = 80;

    var gaugeRenderer = new cloudninja.metering.charting.GaugeRenderer(maxGaugeValue, gaugeThreshold);

    if (isMonthYearCurrent) {
        cloudninja.metering.services.DatabaseService.getCurrentSize(tenantId, function (result, data) {
            gaugeRenderer.render('gauge1', 'KB', data);
        });

        cloudninja.metering.services.StorageService.getCurrentSize(tenantId, function (result, data) {
            gaugeRenderer.render('gauge2', 'KB', data);
        });
    } else {
        cloudninja.metering.services.DatabaseService.getMonthlyAverage(tenantId, year, month, function (result, data) {
            gaugeRenderer.render('gauge1', 'KB', data);
        });

        cloudninja.metering.services.StorageService.getMonthlyAverage(tenantId, year, month, function (result, data) {
            gaugeRenderer.render('gauge2', 'KB', data);
        });
    }

    cloudninja.metering.services.BandwidthService.getMonthlyTotal(tenantId, year, month, function (result, data) {
        gaugeRenderer.render('gauge3', 'KB', data);
    });
});
